package com.settlement.system.service.impl;

import com.settlement.system.mapper.TblAkcDifferentReportMapper;
import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.model.entity.common.TblAkcDifferentReport;
import com.settlement.system.service.ITblAkcDifferentReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import com.settlement.system.mapper.BaseMapper;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 爱库存-差异报表 服务实现类
 * @author Ximi
 * @since 2020-06-30
 */
@Service
@Slf4j
public class TblAkcDifferentReportServiceImpl extends BaseServiceImpl implements ITblAkcDifferentReportService {

    @Resource
    private TblAkcDifferentReportMapper tblAkcDifferentReportMapper;

    @Override
    public BaseMapper init() {
        return tblAkcDifferentReportMapper;
    }

    @Override
    public TblAkcDifferentReport getFoot(Map<String, Object> params) throws ServiceException {
        try {
            return tblAkcDifferentReportMapper.getFoot(params);
        } catch (Exception ex){
            log.error("获取合计异常",ex);
            throw new ServiceException("获取合计异常",ex);
        }
    }

    @Override
    public Integer deleteAll() throws ServiceException {
        try {
            return tblAkcDifferentReportMapper.deleteAll();
        } catch (Exception ex){
            log.error("爱库存-差异报表删除全部数据异常",ex);
            throw new ServiceException("爱库存-差异报表删除全部数据异常",ex);
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public <ModelType> Integer saveNoDuplicateList(List<ModelType> list) throws ServiceException, DuplicateKeyException {
        for(ModelType modelType:list){
            TblAkcDifferentReport report = (TblAkcDifferentReport) modelType;
            tblAkcDifferentReportMapper.deleteByActionId(report.getActionId());
            tblAkcDifferentReportMapper.insert(report);
        }
        return list.size();
    }
}

